/*
* using Discovery stm32f4 board and LCD 5110 of Nokia module.
* LCD 5110 module uses SPI connect
* Author : Vo Tuan Anh
* Email  : votuananhs@gmail.com
*/


#include "stdint.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_spi.h"
#include "stm32f4xx.h"

#ifndef _NOKIA_5110_H_
#define _NOKIA_5110_H_

#ifdef __cplusplus
 extern "C" {
#endif /* __cplusplus */

/* You may find a different size screen, but this one is 84 by 48 pixels */
#define LCD_X     84
#define LCD_Y     48


/* SPI */

/* PIN connect LCD */
#define GPIOx                   GPIOB
#define RCC_AHB1Periph_GPIOx    RCC_AHB1Periph_GPIOB
#define PIN_RES                 ((uint16_t)GPIO_Pin_8)
#define PIN_SCE                 ((uint16_t)GPIO_Pin_11)
#define PIN_DC                  ((uint16_t)GPIO_Pin_12)
#define PIN_SCLK                ((uint16_t)GPIO_Pin_13)
#define PIN_SDIN                ((uint16_t)GPIO_Pin_15)
#define GPIO_PinSource_SCLK     ((uint16_t)GPIO_PinSource13)
#define GPIO_PinSource_SDIN     ((uint16_t)GPIO_PinSource15)
/* Channel SPI */
#define SPIx                    SPI2
#define GPIO_AF_SPIx            GPIO_AF_SPI2
#define RCC_APBnPeriph_SPIx     RCC_APB1Periph_SPI2
#define RCC_APBnPeriphClockCmd  RCC_APB1PeriphClockCmd

/* select mode */
#define MODE_COMMAND_LCD_5110   ((uint8_t)0x00)
#define MODE_DATA_LCD_5110      ((uint8_t)0x01)

/* set display configuration */
#define DISPLAY_BLANK_MODE      ((uint8_t)0x08)
#define DISPLAY_NORMAL_MODE     ((uint8_t)0x0C)
#define DISPLAY_SEGMENTS_MODE   ((uint8_t)0x09)
#define DISPLAY_INVERSE_MODE    ((uint8_t)0x0D)

#define ENABLE_LCD5110          ((uint8_t)0xff)
#define DISABLE_LCD5110         ((uint8_t)0x00)
#define MASK_BIT_PB             ((uint8_t)0x04)
#define MASK_BIT_V              ((uint8_t)0x02)
#define MASK_BIT_H              ((uint8_t)0x01)
#define MASK_FUNCTION_SET       ((uint8_t)0x20)
#define SET_VOP_3_3V            ((uint8_t)0x84)
#define TIME_OUT                ((uint16_t)0x0FFF)
#define OFFSET_START_FONT       ((uint8_t)0x20)
#define FONT_6x8                ((uint8_t)6)
#define MASK_POS_X(x)           ((((uint8_t)0x7F)&(x))|((uint8_t)0x80))
#define MASK_POS_Y(y)           ((((uint8_t)0x07)&(y))|((uint8_t)0x40))

/* Variable or Constant */

/* Function LCD */
void Init_LCD5110(uint8_t u8Vop);
void SetMode_LCD5110(uint8_t u8Mode);
void Enable_LCD5110(uint8_t u8Enable);
ErrorStatus SetPosition_LCD5110(uint8_t u8Position_X, uint8_t u8Position_Y);
ErrorStatus WriteCommand_LCD5110(uint8_t u8Command);
ErrorStatus WriteData_LCD5110(uint8_t u8Data);
void PrintFont5x8(uint8_t character);
void PrintString_LCD5110(uint8_t *Str);
void clean_screen(void);

ErrorStatus DisplayNumber(uint8_t u8Number, uint8_t u8LengthDisplay);


#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* _NOKIA_5110_H_ */
